package com.ll.notice.service;

import com.ll.notice.Factory.HospitalFactory;
import com.ll.notice.Factory.impl.ChencunHospitalFactoryImpl;
import com.ll.notice.Factory.impl.DongbeiGuojiHospitalFactoryImpl;
import com.ll.notice.Factory.impl.JianxiangHospitalFactoryImpl;
import com.ll.notice.Factory.impl.ShundeHospitalFactoryImpl;
import com.ll.notice.Utils.DateTimeUtils;
import com.ll.notice.Utils.PropertiesUtils;
import com.ll.notice.dao.ISmsContentMapper;
import com.ll.notice.dao.impl.SmsContentMapperImpl;
import com.ll.notice.entity.SmsNotice;
import com.ll.notice.ws.client.dongguan.ESBService;
import com.ll.notice.ws.client.dongguan.ESBServiceSoap;
import com.ll.notice.ws.client.zhongshansanyuan.SendMessageCommRt;
import com.ll.notice.ws.client.zhongshansanyuan.WebServerForComm;
import com.ll.notice.ws.client.zhongshansanyuan.WebServerForCommSoap;
import org.apache.log4j.Logger;

import java.util.List;
import java.util.Properties;

public class SmsNoticeServiceImpl {

    private final static Logger log = Logger.getLogger(SmsNoticeServiceImpl.class);
    public static final Properties properties = PropertiesUtils.loadProperties("/config.properties");

    static final String hospital = properties.getProperty("hospital");

    public static void main(String args[] ){

        ISmsContentMapper smsContentMapper = new SmsContentMapperImpl();
        List<SmsNotice> smsNotices = smsContentMapper.findSmsContentList();
        if ("dongbeiguoji".equals(hospital)){
            smsNotices.stream().forEach(smsNotice -> {
                HospitalFactory hospitalFactory = new DongbeiGuojiHospitalFactoryImpl();
                hospitalFactory.sendSms(smsNotice);
                smsContentMapper.update(smsNotice.getSmsId());
            });
        }
        if ("shunde".equals(hospital)){
            smsNotices.stream().forEach(smsNotice -> {
                HospitalFactory hospitalFactory = new ShundeHospitalFactoryImpl();
                hospitalFactory.sendSms(smsNotice);
                smsContentMapper.update(smsNotice.getSmsId());
            });
        }
        if ("jianxiang".equals(hospital)) {
            smsNotices.stream().forEach(smsNotice -> {
                HospitalFactory hospitalFactory = new JianxiangHospitalFactoryImpl();
                hospitalFactory.sendSms(smsNotice);
                smsContentMapper.update(smsNotice.getSmsId());
            });
        }
        if ("chencun".equals(hospital)) {
            smsNotices.stream().forEach(smsNotice -> {
                HospitalFactory hospitalFactory = new ChencunHospitalFactoryImpl();
                hospitalFactory.sendSms(smsNotice);
                smsContentMapper.update(smsNotice.getSmsId());
            });
        }
        if ("dongguan".equals(hospital)) {
            log.info("东莞市中心医院发送短信：" + hospital);
            log.info("开始发送短信");
            String str = "<ESBEnvelope xmlns=\"http://ESB.TopSchemaV2\"><ESBHeader><HeaderControl AppCode=\"APP009\" Password=\"oZS1lPA4emEixPN3\" MessageCategory=\"VX_SendMessage\" Version=\"1\" CreateTime=\"请用当前时间\" /></ESBHeader><ESBBody><BodyControl CallType=\"\"/><BusinessRequest><![CDATA[<?xml version=\"1.0\" encoding=\"utf-8\"?><WeChatMessage xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><AppName>测试消息提醒应用名称</AppName><Items><Item><ReceiverCode>";
            String str1 = "</ReceiverCode><Message>";
            String str2 = "</Message><MessageType>text</MessageType></Item></Items></WeChatMessage>]]></BusinessRequest></ESBBody></ESBEnvelope>";
            try{
                ESBService esbService = new ESBService();
                ESBServiceSoap esbServiceSoap = esbService.getESBServiceSoap();
                smsNotices.stream().forEach(smsNotice -> {

                    String xmlMessage = str + smsNotice.getAttendingDoctorId() + str1 + smsNotice.getSmsContent() + str2;
                    log.info("要发送的内容："+ xmlMessage);
                    String s = esbServiceSoap.callESB(xmlMessage);
                    log.info(s);
                    System.out.println(s);
                    smsContentMapper.update(smsNotice.getSmsId());
                });
            } catch (Exception e){
                log.info("有异常" +e);
            }
            log.info("发送短信结束");
        }

        if ("zhongshansanyuan".equals(hospital)) {
            log.info("中山三院发送短信：" + hospital);
            log.info("开始发送短信");
            try{
                WebServerForComm webServerForComm = new WebServerForComm();
                WebServerForCommSoap webServerForCommSoap = webServerForComm.getWebServerForCommSoap();
                SendMessageCommRt sendMessageCommRt = new SendMessageCommRt();
                sendMessageCommRt.setUserName("thygxt");
                sendMessageCommRt.setPassword("thygxt");
                smsNotices.stream().forEach(smsNotice -> {

                    sendMessageCommRt.setDocPhone(smsNotice.getReceiverMobile());
                    sendMessageCommRt.setMsgContent(smsNotice.getSmsContent());
                    String s = webServerForCommSoap.sendMsgComm(sendMessageCommRt);
                    log.info(s);
                    System.out.println(s);
                    smsContentMapper.update(smsNotice.getSmsId());
                });
            } catch (Exception e){
                log.info("有异常" +e);
            }
            log.info("发送短信结束");
        }

        if ("zhizi".equals(hospital)) {
            String now = DateTimeUtils.nowTime();
            String nowAdd15 = DateTimeUtils.nowTimeAdd15();
            log.info("上海质子医院发送短信：" + hospital);
            log.info("开始发送短信");
            String str = "<ESBEnvelope xmlns=\"http://ESB.TopSchemaV2\"><ESBHeader><HeaderControl AppCode=\"APP011\" Password=\"XzV5qVB4\" MessageCategory=\"SPHICA02A001\" Version=\"1\" CreateTime=\""+now+"\" /></ESBHeader><ESBBody><BodyControl CallType=\"\"/><BusinessRequest><![CDATA[<Message AppCode=\"APP011\" Password=\"12345678\" SendModes=\"短信\" Title=\"\" Content=\"";
            String str1 = " NeedReply=\"0\" BizObjID=\"93444\" SenderUserCode=\"dev\" ReceiveUserCode=\"";
            String str2 = " ReceiveMobileNos=\"";
            String str3 = " SendTime=\""+nowAdd15+"\" ExpiredTime=\""+nowAdd15+"\"></Message>]]></BusinessRequest></ESBBody></ESBEnvelope>";
            try{
                com.ll.notice.ws.client.zhizi.ESBService esbService = new com.ll.notice.ws.client.zhizi.ESBService();
                com.ll.notice.ws.client.zhizi.ESBServiceSoap esbServiceSoap = esbService.getESBServiceSoap();
                smsNotices.stream().forEach(smsNotice -> {

                    String xmlMessage = str + smsNotice.getSmsContent() +"\""+ str1 +"\"" + str2 +smsNotice.getReceiverMobile() +"\""+ str3;
                    log.info("要发送的内容："+ xmlMessage);
                    String s = esbServiceSoap.callESB(xmlMessage);
                    log.info(s);
                    System.out.println(s);
                    smsContentMapper.update(smsNotice.getSmsId());
                });
            } catch (Exception e){
                log.info("有异常" +e);
            }
            log.info("发送短信结束");
        }
    }
}
